<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\CarDealer;

use AuctionCore\Account\Back\Employee;
use AuctionCore\Other\Address;
use AuctionCore\AntNest\UserCenter;

/**
 * 车商待认证
 *
 * @author Xmh
 */
class DealerWaitCertify
{
    private $CarDealer;
    private $CarDealerCertifyTraceLog;
    private $CarDealerCertifyLog;
    private $CarDealerSignLog;
    private $Addr;

    public function __construct()
    {
        $this->CarDealer = M('car_dealer');
        $this->CarDealerCertifyTraceLog = M('car_dealer_certify_trace_log');
        $this->CarDealerCertifyLog = M('car_dealer_certify_log');
        $this->CarDealerSignLog = M('car_dealer_sign_log');
        $this->Addr = new Address();
    }

    /**
     * 获取车商待认证列表
     *
     * @param $page
     * @param $limit
     * @param $params
     */
    public function get_wait_certify_list($page, $limit, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $limit = (int)$limit < 1 ? 10 : (int)$limit;
        //搜索
        $map = array();
        if ($params['status'] == 1) { //待认证
            $map['cd.auth_type'] = ['eq', 0];
            $map['cd.wait_auth_trace_status'] = ['eq', 0];
        } elseif ($params['status'] == 2) {  //认证跟踪
            $map['cd.auth_type'] = ['eq', 0];
            $map['cd.wait_auth_trace_status'] = ['eq', 1];
            $map['cdcl.certify_result'] = ['eq', 1];
        } elseif ($params['status'] == 3) {  //跟踪成功
            $map['cd.auth_type'] = ['gt', 0];
            $map['cd.auth_status'] = ['gt', 0];
            $map['cd.wait_auth_trace_status'] = ['eq', 3];
            $map['cdcl.certify_result'] = ['eq', 3];
        } elseif ($params['status'] == 4) {  //跟踪失败
            $map['cd.auth_type'] = ['eq', 0];
            $map['cd.wait_auth_trace_status'] = ['eq', 2];
            $map['cdcl.certify_result'] = ['eq', 2];
        }
        if ($params['dealer_id'] > 0) {
            $map['cd.dealer_id'] = intval($params['dealer_id']);
        }
        if ($params['business_city'] > 0) {
            $map['cd.business_city'] = intval($params['business_city']);
        }
        if ($params['phone']) {
            $uc = new UserCenter();
            $info1 = $uc->info_bymobile($params['phone'],2);
            $map['du.uid'] = ['eq',$info1['account_id']];
            //$map['cd.contact_mobile'] = ['LIKE', '%' . $params['phone'] . '%'];
        }
        //客户经理
        if ($params['manager_id'] > 0) {
            $map['dm.emp_id'] = intval($params['manager_id']);
        }
        if ($params['start_time']) {
            if ($params['status'] == 1) {
                $map['cd.createtime'] = array('egt', $params['start_time'] . ' 00:00:00');
            } elseif ($params['status'] == 2) {
                $map['cdcl.next_trace_time'] = array('egt', $params['start_time'] . ' 00:00:00');
            } elseif ($params['status'] == 3) {
                $map['cd.apply_auth_time'] = array('egt', $params['start_time'] . ' 00:00:00');
            } elseif ($params['status'] == 4) {
                $map['cdcl.createtime'] = array('egt', $params['start_time'] . ' 00:00:00');
            }
        }
        if ($params['end_time']) {
            if ($params['status'] == 1) {
                $map['cd.createtime'] = array('egt', $params['end_time'] . ' 23:59:59');
            } elseif ($params['status'] == 2) {
                $map['cdcl.next_trace_time'] = array('egt', $params['end_time'] . ' 23:59:59');
            } elseif ($params['status'] == 3) {
                $map['cd.apply_auth_time'] = array('egt', $params['end_time'] . ' 23:59:59');
            } elseif ($params['status'] == 4) {
                $map['cdcl.createtime'] = array('egt', $params['end_time'] . ' 23:59:59');
            }
        }
        if ($params['start_time'] && $params['end_time']) {
            if ($params['status'] == 1) {
                $map['cd.createtime'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
            } elseif ($params['status'] == 2) {
                $map['cdcl.next_trace_time'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
            } elseif ($params['status'] == 3) {
                $map['cd.apply_auth_time'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
            } elseif ($params['status'] == 4) {
                $map['cdcl.createtime'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
            }
        }
        $cols = 'cd.dealer_id,cd.business_city,cd.auth_type,du.uid,
                cd.apply_auth_time,cd.createtime as reg_date,dm.emp_id,
                cdcl.next_trace_time,cdcl.certify_failed_remark,cdcl.updatetime as certify_failed_time';
        $prefix = C('DB_PREFIX');
        if ($params['status'] == 1) {
            $order = 'cd.createtime DESC';
        } elseif ($params['status'] == 2) {
            $order = 'cdcl.next_trace_time DESC';
        } elseif ($params['status'] == 3) {
            $order = 'cd.apply_auth_time DESC';
        } elseif ($params['status'] == 4) {
            $order = 'cdcl.createtime DESC';
        } else {
            $order = 'dealer_id DESC';
        }
        $total = $this->CarDealer->alias('cd')
            ->join($prefix . "dealer_user as du ON du.dealer_id = cd.dealer_id")
            ->join(" LEFT JOIN " . $prefix . "dealer_manager as dm ON cd.dealer_id = dm.dealer_id")
            ->join(" LEFT JOIN " . $prefix . "car_dealer_certify_log as cdcl ON cdcl.dealer_id = cd.dealer_id")
            ->where($map)->count();
        $list = $this->CarDealer->alias('cd')->field($cols)
            ->join($prefix . "dealer_user as du ON du.dealer_id = cd.dealer_id")
            ->join(" LEFT JOIN " . $prefix . "dealer_manager as dm ON cd.dealer_id = dm.dealer_id")
            ->join(" LEFT JOIN " . $prefix . "car_dealer_certify_log as cdcl ON cdcl.dealer_id = cd.dealer_id")
            ->where($map)
            ->limit($limit)
            ->page($page)
            ->order($order)
            ->select();
        //echo M()->_sql();
        if(!empty($list)){
            $dealer = new Dealer();
            $list = $dealer->get_contact_mobile_byAntNest($list);
        }
        if (!empty($list)) {
            foreach ($list as $key => $val) {
                if (!empty($val['emp_id'])) {
                    $employee = \AuctionCore\Account\Back\Employee::user_info_id($val['emp_id']);
                    $list[$key]['dealer_manager'] = $employee['real_name'] ? $employee['real_name'] : "未指定";
                } else {
                    $list[$key]['dealer_manager'] = "未指定";
                }
                $list[$key]['business_city_name'] = $val['business_city'] ? $this->Addr->get_name_by_code($val['business_city']) : '未指定';
                $list[$key]['auth_type'] = C('DEALER_CERTIFY_TYPE')[$val['auth_type']];
                $list[$key]['next_trace_time'] = $val['next_trace_time'] ? $val['next_trace_time'] : '--';
            }

        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取车商待认证详情
     *
     * @param int $dealer_id
     * return array
     */
    public function get_wait_certify_detail($dealer_id)
    {
        if ($dealer_id < 1) {
            return false;
        }
        $condition['cd.dealer_id'] = ['eq', $dealer_id];
        //要查询的列表
        $cols = 'cd.dealer_id,du.uid,cd.business_city,cd.auth_type,
                 cd.createtime as reg_date,dm.emp_id';
        $prefix = C('DB_PREFIX');
        $res = $this->CarDealer->alias('cd')->field($cols)
            ->join($prefix . "dealer_user as du ON du.dealer_id = cd.dealer_id")
            ->join(" LEFT JOIN " . $prefix . "dealer_manager as dm ON cd.dealer_id=dm.dealer_id")
            ->where($condition)->find();
        if(!empty($res)){
            $d = new Dealer();
            $res = $d->get_contact_mobile_byAntNestfind($res);
        }
        $res['business_city_name'] = $res['business_city'] ? $this->Addr->get_name_by_code($res['business_city']) : '未指定';
        if ($res['emp_id'] > 0) {
            $manager = Employee::user_info_id($res['emp_id']);
            $res['manager_name'] = $manager['real_name'];
        } else {
            $res['manager_name'] = '未指定';
        }
        return $res;
    }

    /**
     * 获取车商待认证跟踪日志
     *
     * @param int $dealer_id
     * return array
     */
    public function get_wait_certify_trace_log($dealer_id)
    {
        if ($dealer_id < 1) {
            return false;
        }
        $condition['dealer_id'] = ['eq', $dealer_id];
        $condition['trace_type'] = ['eq', 1];
        $cols = 'emp_id,remark,createtime';
        $list = $this->CarDealerCertifyTraceLog->field($cols)->where($condition)->select();
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $employee = \AuctionCore\Account\Back\Employee::user_info_id($v['emp_id']);
                $list[$k]['emp_name'] = $employee['real_name'];
            }
        }
        return $list;
    }


    /**
     * 添加车商待认证日志
     *
     * @param $dealer_id
     * @param $certify_log
     */
    public function add_wait_certify_log($dealer_id, $certify_log)
    {
        if (empty($dealer_id) || !is_array($certify_log)) {
            return false;
        }
        $certify_log['updatetime'] = date('Y-m-d H:i:s');
        $count = $this->CarDealerCertifyLog->where(['dealer_id' => $dealer_id])->count();
        if ($count > 0) {
            $certify_log_id = $this->CarDealerCertifyLog->where(['dealer_id' => $dealer_id])->getField('id');
            $certify_log['id'] = $certify_log_id;
            return $this->CarDealerCertifyLog->save($certify_log);
        }
        return $this->CarDealerCertifyLog->add($certify_log);
    }

    /**
     * 添加车商跟踪日志
     *
     * @param $certify_trace_log
     */
    public function add_certify_trace_log($certify_trace_log)
    {
        if (!is_array($certify_trace_log)) {
            return false;
        }
        return $this->CarDealerCertifyTraceLog->add($certify_trace_log);
    }


    /**
     * 获取车商认证签约跟踪日志
     *
     * @param int $dealer_id
     * @param int $type 1、待认证 2、个人签约 3、企业签约 4、企业认证审核
     *                  return array
     */
    public function get_certify_trace_log($dealer_id, $type)
    {
        if ($dealer_id < 1) {
            return false;
        }
        $condition['dealer_id'] = ['eq', $dealer_id];
        $condition['trace_type'] = ['eq', $type];
        $cols = 'emp_id,remark,createtime';
        $list = $this->CarDealerCertifyTraceLog->field($cols)->where($condition)->select();
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $employee = \AuctionCore\Account\Back\Employee::user_info_id($v['emp_id']);
                $list[$k]['emp_name'] = $employee['real_name'];
            }
        }
        return $list;
    }

    /**
     * 添加车商签约日志
     *
     * @param $dealer_id
     * @param $sign_log
     * @param $type 1、个人签约 2、企业签约
     */
    public function add_dealer_sign_log($dealer_id, $sign_log, $type)
    {
        if (empty($dealer_id) || !is_array($sign_log)) {
            return false;
        }
        $sign_log['updatetime'] = date('Y-m-d H:i:s');
        $sign_log['sign_type'] = $type;
        $condition['dealer_id'] = ['eq', $dealer_id];
        $condition['sign_type'] = ['eq', $type];
        $count = $this->CarDealerSignLog->where($condition)->count();
        if ($count > 0) {
            $sign_log_id = $this->CarDealerSignLog->where($condition)->getField('id');
            $sign_log['id'] = $sign_log_id;
            return $this->CarDealerSignLog->save($sign_log);
        }
        return $this->CarDealerSignLog->add($sign_log);
    }
}
